package cease.command.message;

import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.Option;

import cease.butter.MessageExecutor;
import cease.command.HasOptionCommand;
import cease.console.PrintHelper;
import cease.urs.Session;
import cease.vo.Message;

/**
 * @author dhf
 */
public class SendMessageCommand extends HasOptionCommand {
    @Option(name = "-t", usage = "text, message want to send", metaVar = "required", required = true)
    private String text;

    @Option(name = "-n", usage = "nickname, the user who should recieve this message", metaVar = "required")
    private String user;

    private void reset() {
        text = null;
        user = null;
    }

    public void run(Session session, String[] args) throws Exception {
        try {
            parser.parseArgument(args);
            Message message = MessageExecutor.getInstance().create(session,
                    user, text);
            PrintHelper.printMessage(message);
        } catch (CmdLineException e) {
            parser.printUsage(System.out);
        } finally {
            reset();
        }
    }

    public String getCmdDesc() {
        return "send a direct message";
    }
}
